<template>
  <div class="large-file-segment-upload-container bg-sky-300">
    <div>
      <el-upload
        ref="uploadRef"
        action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
        :limit="1"
        :on-exceed="handleExceed"
        :auto-upload="false"
      >
        <template #trigger>
          <el-button type="primary">选择文件</el-button>
        </template>
        <el-button type="success" @click="submitUpload" class="ml-4">上传到服务器</el-button>
        <template #tip>
          <div>只允许选择一个文件, 选择新文件会覆盖旧文件</div>
        </template>
      </el-upload>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { genFileId } from "element-plus"
import type { UploadInstance, UploadProps, UploadRawFile } from "element-plus"

const uploadRef = ref<UploadInstance>()

const handleExceed: UploadProps["onExceed"] = (files) => {
  uploadRef.value!.clearFiles()
  const file = files[0] as UploadRawFile
  file.uid = genFileId()
  uploadRef.value!.handleStart(file)
}

const submitUpload = () => {
  uploadRef.value!.submit()
}
</script>

<style lang="scss" scoped>
.large-file-segment-upload-container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
